package com.etpjava.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.etpjava.dao.RewardDao;
import com.etpjava.model.Employee;
import com.etpjava.model.Reward;
import com.etpjava.util.Page;

public class RewardDaoImpl implements RewardDao{
	
	public Session getSession() {
		Configuration cfg = new Configuration();
		SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
		return sessionFactory.openSession();
	}
	
	@Override
	public Reward findById(int id) {
		Reward reward=null;
		Session session = this.getSession();
		session.beginTransaction();
		reward=(Reward) session.get(Reward.class, id);
		session.close();
		return reward;
	}
	
	@Override
	public List<Reward> findAll() {
		return this.findAll(0);
	}
	
	@Override
	public List<Reward> findAll(int st) {
		Session session = this.getSession();
		session.beginTransaction();
		String sql = "select {r.*} from tb_cj {r}  LIMIT "+st+",15";
		Query query=session.createSQLQuery(sql).addEntity("r",Reward.class);
		List<Reward>rewards = (List<Reward>)query.list();
		session.close();
		return  rewards;
	}

	@Override
	public List<Reward> findByTitle(String title) {
		
		return this.findByTitle(title , 0);
	}

	@Override
	public List<Reward> findByTitle(String title ,int st) {
		Session session = this.getSession();
		session.beginTransaction();
		String sql = "select {r.*} from tb_cj {r} where title = " + title +  " LIMIT "+st+",15";
		Query query=session.createSQLQuery(sql).addEntity("r",Reward.class);
		session.close();
		return (List<Reward>)query.list();
	}
	
	@Override
	public List<Reward> findByType(String type) {
		
		return this.findByTitle(type , 0);
	}

	@Override
	public List<Reward> findByType(String type ,int st) {
		Session session = this.getSession();
		session.beginTransaction();
		String sql = "select {r.*} from tb_cj {r} where title = " + type +  " LIMIT "+st+",15";
		Query query=session.createSQLQuery(sql).addEntity("r",Reward.class);
		session.close();
		return (List<Reward>)query.list();
	}
	
	@Override
	public List<Reward> findByTime(Date time) {
		
		return this.findByTime(time , 0);
	}
	
	@Override
	public List<Reward> findByTime(Date time ,int st) {
		Session session = this.getSession();
		session.beginTransaction();
		String sql = "select {r.*} from tb_cj {r} where time = " + time + " LIMIT "+st+",15";
		Query query=session.createSQLQuery(sql).addEntity("r",Reward.class);
		session.close();
		return (List<Reward>)query.list();
	}
	
	@Override
	public List<Reward> findByContent(String content) {
		
		return this.findByTitle(content , 0);
	}

	@Override
	public List<Reward> findByContent(String content ,int st) {
		Session session = this.getSession();
		session.beginTransaction();
		String sql = "select {r.*} from tb_cj {r} where title = " + content +  " LIMIT "+st+",15";
		Query query=session.createSQLQuery(sql).addEntity("r",Reward.class);
		session.close();
		return (List<Reward>)query.list();
	}

	@Override
	public List<Reward> findByPenalty(float penalty) {
		
		return this.findByPenalty(penalty , 0);
	}

	@Override
	public List<Reward> findByPenalty(float penalty ,int st) {
		Session session = this.getSession();
		session.beginTransaction();
		String sql = "select {r.*} from tb_cj {r} where title = " + penalty +  " LIMIT "+st+",15";
		Query query=session.createSQLQuery(sql).addEntity("r",Reward.class);
		session.close();
		return (List<Reward>)query.list();
	}

	
	
	@Override
	public void delete(Reward cj) {
		Session session = this.getSession();
		session.beginTransaction();
		session.delete(cj);
		session.getTransaction().commit();
		session.close();		
	}
	
	@Override
	public void save(Reward cj) {
		Session session = this.getSession();
		session.beginTransaction();
		try {
			session.save(cj);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			session.getTransaction().rollback();
		}
		finally{
			session.close();	
		}	
	}
	
	@Override
	public void update(Reward cj) {
		Session session = this.getSession();
		session.beginTransaction();
		
			session.update(cj);
			session.getTransaction().commit();
		
			session.close();
		}
	
	public void getPagelist(Page aPage) {
		Session session = this.getSession();
		session.beginTransaction();
		// int rowcount=((Integer)
		// session.createCriteria(Employee.class).setProjection(Projections.rowCount()).uniqueResult()).intValue();
		// int a=(Integer)
		// session.createCriteria(Employee.class).setProjection(Projections.rowCount()).uniqueResult();
		int rowcount = session.createSQLQuery("select id from tb_cj")
				.list().size();
		aPage.setRowTotalCount(rowcount);
		String sql = "select {cj.*} from tb_cj {cj}  LIMIT "
				+ aPage.getRowStartCount() + "," + Page.pagesize;
		Query query = session.createSQLQuery(sql)
				.addEntity("cj", Reward.class);
		session.getTransaction().commit();
		List cj = query.list();
		aPage.setList(cj);
		session.close();
	}
	
	
	
	
	
	}
